/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//-------------------------------------------------------------
//-------------------------------------------------------------
ajax.Instances = null;
//-------------------------------------------------------------
ajax.CreateObject = function(config) {
    var obj = null;
    if (ajax.Instances != null) {
        obj = ajax.Instances[config.id];
    }
    if (obj == null) {
        if (ajax.Instances == null) {
            ajax.Instances = new Object();
        }
        obj = new ajax(config);
        ajax.Instances[config.id] = obj;
    }
    obj.init();

    return obj;
}
//-------------------------------------------------------------
function ajax(config) {
    var Me = this;
    this.id = config.id;
    this.data = "";
    this.url = "";
    this.method = 'POST';
    this.success = function(data) {
    };
    //------------------------------------------------------------
    this.LoadingAjax = function(ContentId) {
        jQuery(ContentId).html("<center><img src='/widgets/images/wait.gif' border='0' /></center>");
    }
    this.loading = function(act) {
        if (act) {
            jQuery(".headerloader0125").show();
        } else {
            jQuery(".headerloader0125").hide();
        }
    }
    //------------------------------------------------------------
    this.ClosePopup = function() {
        Me.roll.hide();
    }
    //------------------------------------------------------------
    this.showAjax = function(event, url, data, on_success, method) {
        if (typeof (event) != 'undefined') {
            jQuery(event).attr("disabled", "disabled");
        }

        if (jQuery.trim(data) == "") {
            data = "ajax=1";
        } else {
            data += "&ajax=1";
        }

        $.ajax({
            type: method,
            url: url,
            data: data,
            success: function(data) {
                if (data == "Authentication Fail") {
                    location.reload();
                }
                on_success(data);
                if (typeof (event) != 'undefined') {
                    jQuery(event).removeAttr("disabled");
                }
            },error:function(data){
                alert("has a error, please reload or enter F5 to reload page.");
            }
        });
    }
    //------------------------------------------------------------
    this.show = function(event) {
        Me.showAjax(event, Me.url, Me.data, Me.success, Me.method);
    }
    this.toAlias = function(str) {
        str = str.replace(/^\s+|\s+$/g, ''); //trim
        str = str.toLowerCase();
        var from = "àáạảãâầấậẩẫăằắặẳẵèéẹẻẽêềếệểễìíịỉĩòóọỏõôồốộổỗơờớợởỡùúụủũưừứựửữỳýỵỷỹđ";
        var to = "aaaaaaaaaaaaaaaaaeeeeeeeeeeeiiiiiooooooooooooooooouuuuuuuuuuuyyyyyd";
        for (var i = 0, l = from.length; i < l; i++) {
            str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
        }
        str = str.replace(/[^a-z0-9\s-]/g, '') // remove invalid chars
                .replace(/\s+/g, '-') // collapse whitespace and replace by -
                .replace(/-+/g, '-'); // collapse dashes
        return str;
    }

    this.init = function() {

    }

}
var _ajax = null;

_ajax = ajax.CreateObject({
    id: '_ajax'
});
